//
//  LZAlertNormalTextCustomView.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/15.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZAlertNormalTextCustomView.h"
#import "Masonry.h"

@interface LZAlertNormalTextCustomView ()

@property (nonatomic, strong) UILabel *labMessage;

@property (nonatomic, assign) float lab_padding_left;   // label到左边的间距
@property (nonatomic, assign) float lab_padding_top; // label到底边的间距

@end

@implementation LZAlertNormalTextCustomView

+(LZAlertBaseCustomView*)initNormalViewWithMessage:(NSString*)aString {
    LZAlertNormalTextCustomView *aView = [[LZAlertNormalTextCustomView alloc] initWithFrame:CGRectMake(0, 0, 320-60, 100)];
    [aView initData];
    [aView createRemindMessage:aString];
    
    [aView resetCustomHeight];
    
    return aView;
}

-(void)initData {
    self.lab_padding_left = 25;
    self.lab_padding_top = 35;
}

-(void)createRemindMessage:(NSString*)aString {
    _labMessage = [[UILabel alloc] init];
    self.labMessage.numberOfLines = 0;
    self.labMessage.textColor = UIColorFromRGB(0x333333, 1);
    [self addSubview:_labMessage];
    
    NSDictionary * dicAttributes = [self labMessageAttributes];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:aString attributes:dicAttributes];
    self.labMessage.attributedText = attribute;
    
    [self.labMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(self.lab_padding_top);
        make.leading.equalTo(self.mas_leading).offset(self.lab_padding_left);
        make.trailing.equalTo(self.mas_trailing).offset(-self.lab_padding_left);
    }];
    
}

// 根据子视图内容重置本view的高度
-(void)resetCustomHeight {
    NSDictionary *attributes = [self labMessageAttributes];
    
    float labHeight = [self getTextHeightWithText:self.labMessage.text Attributes:attributes andWidth:self.customWidth-self.lab_padding_left*2];
    
    float totalHeight = labHeight+self.lab_padding_top*2;
    
    self.frame = CGRectMake(0, 0, 0, totalHeight);
}

// 设置文字提示的attributes并返回
-(NSDictionary*)labMessageAttributes {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName: [UIFont systemFontOfSize:16],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    return attributes;
}

// 根据文字内容和attributes、lab的宽度获取文字高度
- (CGFloat)getTextHeightWithText:(NSString *)text Attributes:(NSDictionary *)attributes andWidth:(float)width {
    
    
    CGRect expectedLabelRect = [text boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:attributes
                                                  context:nil];
    return CGRectGetHeight(expectedLabelRect);
    
}

@end
